use "$raw/s4_background_noPII.dta", clear

*******cleaning******
*orphan
gen orphan=0
replace orphan=1 if parents!="Both Alive"
*replace orphan=1 if parents=="Both dead" | parents=="Mother alive"
la var orphan "Orphan"
gen mother_raised=0
replace mother_raised=1 if parents=="Mother alive"


*school
tab PLEresult
_pctile PLEresult if PLEresult!=.
return list 
gen below_median_PLE=0 if PLEresult!=.
replace below_median_PLE=1 if PLEresult>r(r1) & PLEresult!=. // higher scores are worse
la var below_median_PLE "Below median PLE"

gen below_median_school=0 if (PLEresult!=. )
replace below_median_school=1 if below_median_PLE==1 
la var below_median_school "Below median prior school"


*kampala 
replace parish=lower(parish)
replace subcounty=lower(subcounty)
replace district=lower(district)

gen kampala=0
replace kampala=1 if  parish=="kampala" | subcounty=="kampala" | district=="kampala"
la var kampala "From Kampala"
tab kampala
//20% from kampala


*An indicator for whether either parent had a professional occupation (teacher, accountant, lawyer, doctor, engineer, civil servant)
replace fathersoccupation=lower(fathersoccupation )
replace mothersoccupation=lower(mothersoccupation )

local professional "accountant architect auditor building engineer "civil engineer" "civil servant" "councillor lc 3" "district planner" doctor "electrical engineer" engineer lawyer "lc2 chairman" lecturer mayor "principal researcher" teacher  "technical engineer" coucellor banker "head mistress" journalist researcher "school burser""
gen profession_dad=0
gen profession=0
gen profession_mum=0
foreach x of local professional {
replace profession=1 if mothersoccupation=="`x'" | fathersoccupation=="`x'"
replace profession_dad=1 if fathersoccupation=="`x'"
replace profession_mum=1 if mothersoccupation=="`x'" 

}
la var profession "One or both parents professionals"

**. An indicator for whether financial support was being provided to the student
gen bursary=0
replace bursary=1 if bursarygranted=="yes"
la var bursary "Received a bursary"

save "$cleaned/s4_background_cleaned.dta", replace

//combine with s4 full data
use "$cleaned/all_s4_cleaned.dta", clear
merge 1:1 id using "$cleaned/s4_background_cleaned.dta"
drop if _merge==1

**************************************
*	table A1 
**************************************

global subgroup below_median_school kampala mother_raised    profession bursary
*local subgroup  profession 
foreach x of global subgroup { 

sum `x' if treatment==0
sum `x' if treatment==1
capture gen treatment_`x'=treatment*`x'
local label: var la `x'
label variable treatment_`x' "Treatment * `label'"

}

eststo placebo:  estpost summarize ///
    $subgroup if treatment == 0
eststo treatment:  estpost summarize ///
    $subgroup if treatment == 1
eststo diff:  estpost ttest ///
   $subgroup, by(treatment) unequal
   esttab  placebo treatment diff using "$output/table_A1.tex", ///
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2))  p(pattern(0 0 1) par fmt(2))") ///
label replace



**************************************
*	table A12
**************************************

*qval 
preserve 
foreach y of global subgroup  {
local i=1
tempfile tf`y'`i' 
reg std_MTC treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg MTC_1 treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
}

local j=1
foreach y of global subgroup  {
dsconcat `tf`y'1' `tf`y'2' 
keep if parm=="treatment_`y'"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tbq`j'`x'=q[`x']
display ${Tbq`j'`x'}
}

dsconcat `tf`y'1' `tf`y'2' 
keep if parm=="treatment"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq`j'`x'=q[`x']
display ${Tq`j'`x'}
}
local j=`j'+1
}
restore



local i=1 
foreach x of global subgroup { 

reghdfe std_MTC  std_MTC_mock  treatment  treatment_`x'  school1-school8  $controls `x', vce(robust)  $reghdfe
sum std_MTC if treatment==0 & `x'==1
local mean=r(mean)
sum std_MTC if treatment==0 & `x'==0
local mean2=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A12.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(MTC_1 treatment treatment_`x' `x'  ${controls} std_MTC_mock) addstat(mean, `mean', mean2, `mean2', treated, `F1c',SE , `F1se', p_value, `p_value', qvalue, ${Tq`i'1}, qvalue interaction,${Tbq`i'1} ) label adec(2)

reghdfe MTC_1  std_MTC_mock  treatment  treatment_`x'  school1-school8  $controls `x', vce(robust)  $reghdfe
sum MTC_1 if treatment==0 & `x'==1
local mean=r(mean)
sum MTC_1 if treatment==0 & `x'==0
local mean2=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A12.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(MTC_1 treatment treatment_`x' `x'  ${controls} std_MTC_mock) addstat(mean, `mean', mean2, `mean2', treated, `F1c',SE , `F1se', p_value, `p_value',  qvalue, ${Tq`i'2}, qvalue interaction,${Tbq`i'2} ) label adec(2)


local i=`i'+1
}




